package 优先算法.双指针.和为S的两个数;

import java.util.Arrays;

/**
 * name 查找总价格为目标值的两个商品
 * <a href="https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/description/"
 */
public class TwoNum {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] price = {3, 9, 12, 15};
        int target = 18;
        int[] rel=so.twoSum(price, target);
        System.out.println(Arrays.toString(rel));
    }
}
class Solution {
    public int[] twoSum(int[] price, int target) {
        int[] rel=new int[2];
        int left=0,right=price.length-1;
        while(left < right){
            if(price[left]+price[right]<target){
                left++;
            }else if(price[left]+price[right]>target){
                right--;
            }else{
                rel[0]=price[left];
                rel[1]=price[right];
                break;
            }
        }
        return rel;
    }
}